# Contributor: David Huffman <storedbox@outlook.com>
# Maintainer:
pkgname=libdwarf
pkgver=20210528
pkgrel=0
pkgdesc="Parsing library for DWARF2 and later debugging file format"
url="https://www.prevanders.net/dwarf.html"
arch="all"
license="LGPL-2.1-or-later"
makedepends="elfutils-dev zlib-dev"
subpackages="$pkgname-static $pkgname-dev dwarf-tools dwarf-tools-doc"
source="http://www.prevanders.net/libdwarf-$pkgver.tar.gz"

build() {
	[ "$CARCH" != "s390x" ] && _enable_dwarfgen="--enable-dwarfgen"
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--enable-shared \
		$_enable_dwarfgen
	make
}

check() {
	make check
}

package() {
	local bindir="$pkgdir/usr/bin"
	local docdir="$pkgdir/usr/share/doc/dwarf-tools"
	local incdir="$pkgdir/usr/include/libdwarf"
	local libdir="$pkgdir/usr/lib"
	local man1dir="$pkgdir/usr/share/man/man1"
	mkdir -p "$bindir" "$docdir" "$incdir" "$libdir" "$man1dir"

	cd "$builddir/libdwarf"
	cp dwarf.h libdwarf.h "$incdir"
	cd "$builddir/libdwarf/.libs"
	cp libdwarf.a libdwarf.so libdwarf.so.1 "$libdir"

	cd "$builddir/dwarfdump"
	cp .libs/dwarfdump "$bindir"
	cp COPYING "$docdir/dwarfdump.COPYING"
	cp DWARFDUMPCOPYRIGHT GPL.txt "$docdir"
	cp dwarfdump.conf "$libdir"
	cp dwarfdump.1 "$man1dir"

	case "$CARCH" in
		s390x) ;;
		*)
		cd "$builddir/dwarfgen"
		cp .libs/dwarfgen "$bindir"
		cp COPYING "$docdir/dwarfgen.COPYING"
		cp dwarfgen.1 "$man1dir"
		;;
	esac
}

tools() {
	license="GPL-2.0-only AND BSD-3-Clause"
	pkgdesc="Tools for interacting with DWARF2 and later debugging files"

	local bin="usr/bin"
	local lib="usr/lib"

	mkdir -p "$subpkgdir/$lib"
	mv "$pkgdir/$bin" "$subpkgdir/$bin"
	mv "$pkgdir/$lib/dwarfdump.conf" "$subpkgdir/$lib"
}

static() {
	depends=""
	pkgdesc="$pkgdesc (static library)"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/*.a "$subpkgdir"/usr/lib
}

sha512sums="
e0f9c88554053ee6c1b1333960891189e7820c4a4ddc302b7e63754a4cdcfc2acb1b4b6083a722d1204a75e994fff3401ecc251b8c3b24090f8cb4046d90f870  libdwarf-20210528.tar.gz
"
